package com.ymy.thinking.in.spring.bean.definition;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import thinking.in.spring.ioc.overview.domain.User;

/**
 * Bean 别名示例
 *
 * @author Ringo
 * @date 2021/8/23 22:07
 */
public class BeanAliasDemo {

    public static void main(String[] args) {
        // 1: 创建 IoC 容器 加载配置元信息
        String configLocation = "classpath:META-INF/bean-definitions-context.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(configLocation);

        // 2: 依赖查找
        // 通过 Bean Name 查找
        User user = (User) applicationContext.getBean("user");
        // 通过 Bean Alias 查找
        User ringo = (User) applicationContext.getBean("ringo-user");
        System.out.println("user: " + user);
        System.out.println("ringo: " + ringo);
        System.out.println("user == ringo ? " + (user == ringo));     // true
    }
}
